﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;

namespace HQWeb.SysManage.UserManage
{
    public partial class Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                bindDepartmentList();
                Boolean flag = Convert.ToBoolean(Session["IsAll"]);
                if (flag)            //是否可以管理所有分店用户
                {
                    bindBaranchList();
                    br1.Style["display"] = "";
                }
                else
                {
                    bindBaranchList();
                    br2.Style["display"] = "";
                }

                string strType = Request["sign"].Trim();
                txtSign.Text = strType;
                if (strType.Equals("add"))  //添加
                {
                    string strPID = Request["pid"].Trim();
                    if (drop_Department.Items.FindByValue(strPID) != null)   //选择部门
                    {
                        drop_Department.SelectedValue = strPID;
                    }
                    string strBID = Request["bid"].Trim();           //显示分店
                    if (flag)
                    {
                        if (drop_Branch.Items.FindByValue(strBID) != null)
                        {
                            drop_Branch.SelectedValue = strBID;
                        }
                    }
                    else
                    {
                        txtBrdHid.Text = strBID;
                        BLL.SysManage.T_PUB_BRANCH bll = new BLL.SysManage.T_PUB_BRANCH();
                        int temp;
                        int.TryParse(strBID, out temp);
                        Model.SysManage.T_PUB_BRANCH model = bll.GetModel(temp);
                        labBR.Text = model.BRANCH_NAME;
                        drop_Branch.SelectedValue = model.BRANCH_ID.ToString();
                    }
                    btnSetPassWord.Visible = false;
                }
                else if (strType.Equals("edit"))     ///编辑
                {
                    string strID = Request["id"].Trim();
                    hidEditID.Text = strID;
                    FillPage(strID, flag);
                }
            }
        }

        #region 页面填充
        private void FillPage(string strID, Boolean flag)
        {
            BLL.SysManage.T_PUB_USER bll = new BLL.SysManage.T_PUB_USER();
            int temp;
            int.TryParse(strID, out temp);
            Model.SysManage.T_PUB_USER model = bll.GetModel(temp);
            if (model != null)
            {
                txtUserName.Text = model.USER_NAME;
                txtRealName.Text = model.NAME;
                drop_Department.SelectedValue = model.DEPARTMENT_ID.ToString();
                if (flag)                                                 //是否可以管理所有分店用户
                {
                    drop_Branch.SelectedValue = model.BRANCH_ID.ToString();
                }
                else
                {
                    BLL.SysManage.T_PUB_BRANCH Brbll = new BLL.SysManage.T_PUB_BRANCH();
                    Model.SysManage.T_PUB_BRANCH Brmodel = Brbll.GetModel(Convert.ToInt32(model.BRANCH_ID));
                    labBR.Text = Brmodel.BRANCH_NAME;
                    drop_Branch.SelectedValue = model.BRANCH_ID.ToString();
                }

                txtDescrip.Text = model.POST;
                txtPhone.Text = model.PHONE;
                txtMailBox.Text = model.MAILBOX;
                btnSend.CommandName = "edit";
                btnSend.CommandArgument = strID;
            }
        }

        #endregion

        #region 绑定下拉框
        //部门绑定
        private void bindDepartmentList()
        {
            BLL.SysManage.T_PUB_DEPARTMENT DepartBll = new BLL.SysManage.T_PUB_DEPARTMENT();
            IList il = DepartBll.GetModelList("");
            drop_Department.ClearSelection();
            drop_Department.DataSource = il;
            drop_Department.DataTextField = "DEPARTMENT_NAME"; //dropdownlist的Text的字段 
            drop_Department.DataValueField = "DEPARTMENT_ID";//dropdownlist的Value的字段 
            drop_Department.DataBind();
            ListItem item = new ListItem("无上级", "0");

        }
        //分店绑定
        private void bindBaranchList()
        {
            BLL.SysManage.T_PUB_BRANCH branchBll = new BLL.SysManage.T_PUB_BRANCH();
            List<Model.SysManage.T_PUB_BRANCH> branchModel = branchBll.GetModelList("");
            drop_Branch.DataSource = branchModel;
            drop_Branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_Branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_Branch.DataBind();
        }
        #endregion

        #region 按钮事件

        //保存
        protected void btnSend_Click(object sender, EventArgs e)
        {
            BLL.SysManage.T_PUB_USER bll = new BLL.SysManage.T_PUB_USER();
            Model.SysManage.T_PUB_USER model;
            string strCommand = btnSend.CommandName;
            string strSymbole = txtUserName.Text.Trim();
            #region 判断登入名唯一
            model = bll.GetModelByUserSymbol(strSymbole);
            if (model != null && hidEditID.Text.Trim() != model.USER_ID.ToString() && model.ISUSED == true)
            {
                Universal.JS.Alert("该用户名已存在！");
                return;
            }
            #endregion
            if (strCommand.Equals("edit"))
            {
                string strID = btnSend.CommandArgument.Trim();
                int temp;
                int.TryParse(strID, out temp);
                model = bll.GetModel(temp);
            }
            if (model == null)
            {
                model = new Model.SysManage.T_PUB_USER();
            }
            model.USER_NAME = txtUserName.Text.Trim();
            model.NAME = txtRealName.Text.Trim();
            model.DEPARTMENT_ID = Convert.ToInt32(drop_Department.SelectedValue);
            if (drop_Branch.SelectedValue != "")
            {
                model.BRANCH_ID = Convert.ToInt32(drop_Branch.SelectedValue);
            }
            model.POST = txtDescrip.Text.Trim();
            model.PHONE = txtPhone.Text.Trim();
            model.MAILBOX = txtMailBox.Text.Trim();
            model.ISUSED = true;
            if (strCommand.Equals("edit"))
            {
                bll.Update(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + model.USER_NAME + "--用户", DateTime.Now);
            }
            else
            {
                model.PASSWORD = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtUserName.Text.Trim(), "MD5").Substring(0, 18);
                model.TIME = DateTime.Now;
                bll.Add(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加" + model.USER_NAME + "--用户", DateTime.Now);
            }
            //string strUrl = string.Format(" List.aspx?pid={0}", drop_Department.SelectedValue);
            //Response.Redirect(strUrl);
            Universal.JS.RunJS("GoList('" + drop_Department.SelectedValue + "')");
        }

        //密码初始化
        protected void btnSetPassWord_Click(object sender, EventArgs e)
        {
            BLL.SysManage.T_PUB_USER bll = new BLL.SysManage.T_PUB_USER();
            Model.SysManage.T_PUB_USER model;
            string strID = btnSend.CommandArgument.Trim();
            int temp;
            int.TryParse(strID, out temp);
            model = bll.GetModel(temp);
            model.PASSWORD = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtUserName.Text.Trim(), "MD5").Substring(0, 18);
            bll.Update(model);
            Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "初始化密码" + model.USER_NAME + "--用户", DateTime.Now);
            Universal.JS.Alert("初始化成功！");
        }

        #endregion



    }
}